#!/bin/bash

set -x

# 提供给TX的用户名、密码、模块
USERNAME=upload
PASSWORD=123456
MODULE=upload

# 我们服务器上的目录
SPATH=/data/upload


CLIENT=127.0.0.1
# 修改默认端口
PORT=6873
# 可以不对外开发，绑定在本机地址
SERVER=127.0.0.1

RSYNC_PWD=/etc/rsyncd.pwd


config_server()
{

	cp /etc/rsyncd.conf /etc/rsyncd.conf.bak

	cat > /etc/rsyncd.conf << EOF
uid = nobody
gid = nobody
use chroot = no
max connections = 100
timeout = 600
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
address = $SERVER
port = $PORT
auth users = $USERNAME
secrets file = $RSYNC_PWD
list = false
ignore errors = yes
transfer logging = yes

[$MODULE]
path = $SPATH
read only = false
hosts allow = $CLIENT
EOF

	echo "$USERNAME:$PASSWORD" > $RSYNC_PWD
	chmod 600 $RSYNC_PWD
	
	mkdir -p $SPATH
	chown nobody:nobody $SPATH
	
	
	systemctl enable rsyncd
	systemctl restart rsyncd
}

config_logrotate()
{
	if [ ! -f /etc/cron.daily/logrotate -o ! -f /etc/logrotate.conf ]; then
		echo "ERROR: miss logrotate && crontab"
		return
	fi

	cat > /etc/logrotate.d/rsync <<EOF
/var/log/rsync.log {
	daily
	maxsize 100M
	rotate 7
	copytruncate
	compress
	delaycompress
	notifempty
	missingok
}
EOF
}

config_server
config_logrotate
